﻿package frEngine.effectEditTool.temple
{
    import __AS3__.vec.*;
    import baseEngine.core.*;
    import baseEngine.system.*;
    import flash.events.*;
    import flash.geom.*;
    import flash.utils.*;
    import frEngine.*;
    import frEngine.core.mesh.*;
    import frEngine.effectEditTool.manager.*;
    import frEngine.event.*;

    public class TempleFight extends Object implements ITemple
    {
        private var _container:Obj3dContainer;
        private var _fighter:Pivot3D;
        private var _fighted:Object;
        private var _weapon:Mesh3D;
        private var _onHitDispose:Boolean;
        private var _keepGoing:Boolean;
        private var _playDringTime:Number;
        private var _callBack:Function;
        private var _fightList:Vector.<FightParams>;
        private var _onFightFrame:int = 0;
        private var hasCalculateEndPosMap:Dictionary;
        private var hasCalculateStartPosMap:Dictionary;
        private var _isOnPlaying:Boolean;
        private var _timeId:int = -1;
        private var _hasHit:Boolean = false;
        public var effecturl:String;
        private var _fightedObj:Pivot3D;
        public static var maxFightedTime:int = 1000;
        private static var _zeroVector3d:Vector3D = new Vector3D();
        private static var _globlePos:Vector3D = new Vector3D();
        private static var _templeVector3d:Vector3D = new Vector3D(0, 50, 0);

        public function TempleFight(param1:Obj3dContainer)
        {
            this.hasCalculateEndPosMap = new Dictionary(false);
            this.hasCalculateStartPosMap = new Dictionary(false);
            this._container = param1;
            this._fightedObj = new Pivot3D("");
            return;
        }// end function

        public function get targetRole() : Pivot3D
        {
            return this._fighter;
        }// end function

        public function setFightParams(param1:Pivot3D, param2, param3:Mesh3D, param4:Boolean, param5:Function = null, param6:int = 0) : void
        {
            var _loc_7:Number = NaN;
            var _loc_8:Vector3D = null;
            var _loc_10:Vector3D = null;
            var _loc_11:int = 0;
            if (param1 == null || param2 == null)
            {
                throw new Error("templeFight 参数 不能为空 ！！");
            }
            this._hasHit = false;
            this._keepGoing = false;
            this._fighter = param1;
            this._weapon = param3;
            this._onHitDispose = param4;
            this._callBack = param5;
            var _loc_9:* = this._fighter.world.position;
            if (param2 is Vector3D)
            {
                _loc_8 = Vector3D(param2);
                this._fightedObj.setPosition(_loc_8.x, _loc_8.y, _loc_8.z, true);
                this._fighted = this._fightedObj;
            }
            else
            {
                this._fighted = param2;
                _loc_8 = Pivot3D(this._fighted).world.position;
            }
            _loc_7 = _loc_8.subtract(_loc_9).length;
            this._container.rehangToTemple();
            if (this._fighted && this._fighted is Pivot3D)
            {
                FrEventDispatcher.instance.proxyAddEventListener(this._fighted, Engine3dEventName.PIVOT3D_DISPOSE, this.toStopHander);
            }
            FrEventDispatcher.instance.proxyAddEventListener(this._fighter, Engine3dEventName.PIVOT3D_DISPOSE, this.toStopHander);
            if (this._container.parent != Global3D.scene)
            {
                Global3D.scene.addChild(this._container);
            }
            if (_loc_7 > 0 && !this._container.isInPool && !this._container.isDisposed)
            {
                _loc_10 = param1.world.position;
                this._container.setPosition(_loc_10.x, _loc_10.y, _loc_10.z);
                this.checkAndPlay();
                if (this._timeId > 0)
                {
                    clearTimeout(this._timeId);
                }
                if (maxFightedTime > -1 && param6 > -1)
                {
                    _loc_11 = maxFightedTime > param6 ? (maxFightedTime) : (param6);
                    this._timeId = setTimeout(this.hasHitHander, _loc_11, true);
                }
            }
            else if (this._onHitDispose)
            {
                this.hasHitHander(true);
            }
            return;
        }// end function

        private function checkHasToDispose() : void
        {
            if (this.effecturl)
            {
                trace("not dispose :", this.effecturl);
            }
            return;
        }// end function

        private function toStopHander(event:Event) : void
        {
            if (this._fighter != null)
            {
                FrEventDispatcher.instance.proxyRemoveEventListener(this._fighter, Engine3dEventName.PIVOT3D_DISPOSE, this.toStopHander);
                FrEventDispatcher.instance.proxyRemoveEventListener(this._fighted, Engine3dEventName.PIVOT3D_DISPOSE, this.toStopHander);
                this._onHitDispose = true;
                this.hasHitHander(true);
            }
            return;
        }// end function

        public function parsersParams(param1) : void
        {
            var _loc_5:FightParams = null;
            if (!param1 || !param1.fightList)
            {
                return;
            }
            this._onFightFrame = param1.fightOnframe;
            var _loc_2:* = param1.fightList;
            this._fightList = new Vector.<FightParams>;
            var _loc_3:* = _loc_2.length;
            var _loc_4:int = 0;
            while (_loc_4 < _loc_3)
            {
                
                _loc_5 = new FightParams(_loc_2[_loc_4]);
                this._fightList.push(_loc_5);
                _loc_4++;
            }
            this.checkAndPlay();
            return;
        }// end function

        public function unHangAll(param1:Pivot3D, param2:Boolean = false) : void
        {
            var _loc_3:int = 0;
            var _loc_4:int = 0;
            var _loc_5:FightParams = null;
            var _loc_6:Pivot3D = null;
            this.hasCalculateStartPosMap = new Dictionary(false);
            this.hasCalculateEndPosMap = new Dictionary(false);
            this._keepGoing = false;
            FrEventDispatcher.instance.proxyRemoveEventListener(this._fighted, Engine3dEventName.PIVOT3D_DISPOSE, this.toStopHander);
            FrEventDispatcher.instance.proxyRemoveEventListener(this._fighter, Engine3dEventName.PIVOT3D_DISPOSE, this.toStopHander);
            if (this._callBack != null)
            {
                this._callBack();
                this._callBack = null;
            }
            this.isOnPlaying = false;
            this._hasHit = false;
            if (this._timeId > 0)
            {
                clearTimeout(this._timeId);
                this._timeId = -1;
            }
            if (this._fightList)
            {
                _loc_3 = this._fightList.length;
                _loc_4 = 0;
                while (_loc_4 < _loc_3)
                {
                    
                    _loc_5 = this._fightList[_loc_4];
                    _loc_6 = this._container.getObject3dByID(_loc_5.uid);
                    if (_loc_6)
                    {
                        _loc_6.fightObjectControler.clear();
                    }
                    _loc_4++;
                }
            }
            if (!param2)
            {
                this._container.removeAllHangToTemple(param1);
            }
            return;
        }// end function

        public function setTempleParams(param1) : void
        {
            this.setFightParams(param1[0], param1[1], param1[2], param1[3]);
            return;
        }// end function

        public function set isOnPlaying(param1:Boolean) : void
        {
            this._isOnPlaying = param1;
            return;
        }// end function

        public function checkAndPlay() : void
        {
            if (!this._fighter || !this._fighted || !this._fightList || this._isOnPlaying || this._hasHit)
            {
                return;
            }
            this.checkFightList();
            this._isOnPlaying = true;
            return;
        }// end function

        private function checkFightList() : void
        {
            var _loc_5:FightParams = null;
            var _loc_6:Pivot3D = null;
            if (this._hasHit)
            {
                return;
            }
            var _loc_1:* = this._container.timerContorler.curFrame;
            var _loc_2:Number = 1000000;
            var _loc_3:* = this._fightList.length;
            var _loc_4:int = 0;
            while (_loc_4 < _loc_3)
            {
                
                _loc_5 = this._fightList[_loc_4];
                if (_loc_5.keepGoing)
                {
                    this._keepGoing = true;
                    this._onHitDispose = false;
                }
                _loc_6 = this._container.getObject3dByID(_loc_5.uid);
                if (_loc_6)
                {
                    _loc_6.fightObjectControler.resetEmmiter(this, this.hasHitHander, _loc_5);
                }
                _loc_4++;
            }
            return;
        }// end function

        private function hasHitHander(param1:Boolean) : void
        {
            this._hasHit = true;
            if (this._timeId > 0)
            {
                clearTimeout(this._timeId);
                this._timeId = -1;
            }
            if (this._fighted is Md5Mesh)
            {
                FrEventDispatcher.instance.proxyDispatchEvent(this._fighted, Engine3dEventName.ONFIGHT, [this._onHitDispose]);
            }
            if (this._callBack != null)
            {
                this._callBack.call();
                this._callBack = null;
            }
            if (this._onHitDispose || param1)
            {
                this.impDispose(param1);
            }
            else if (this._keepGoing)
            {
                this._timeId = setTimeout(this.impDispose, 600, true);
            }
            return;
        }// end function

        private function impDispose(param1:Boolean) : void
        {
            if (this._timeId > 0)
            {
                clearTimeout(this._timeId);
                this._timeId = -1;
            }
            if (this._container)
            {
                this._container.impRemove = param1;
                if (this._container.enabledPool)
                {
                    this._container.dispose(true);
                }
            }
            this.isOnPlaying = false;
            this.effecturl = null;
            return;
        }// end function

        public function getPos(param1:String, param2:String, param3:Boolean, param4:Boolean) : Vector3D
        {
            var _loc_6:Dictionary = null;
            var _loc_11:Vector3D = null;
            if (this._container == null)
            {
                return null;
            }
            var _loc_5:* = param1 + param2;
            if (param3)
            {
                _loc_6 = this.hasCalculateStartPosMap;
            }
            else
            {
                _loc_6 = this.hasCalculateEndPosMap;
            }
            var _loc_7:* = _loc_6[_loc_5];
            if (_loc_7 && _loc_7.w != -1 && !param4)
            {
                return _loc_7;
            }
            var _loc_8:Vector3D = null;
            var _loc_9:* = this.getObj3dByFlag(param1);
            if (!_loc_9)
            {
                return null;
            }
            if (_loc_9.scene == null)
            {
                this._container.parserLayerHang(true);
            }
            var _loc_10:Boolean = false;
            if (param2 && _loc_9 is Md5Mesh)
            {
                _loc_11 = Md5Mesh(_loc_9).targetMd5Controler.getGJointPoseByName(param2);
                if (_loc_11)
                {
                    _loc_8 = _loc_9.world.transformVector(_loc_11);
                }
                else
                {
                    _loc_10 = true;
                }
            }
            else if (param1 == ETempeItemName.FightedName && this._fighted is Vector3D)
            {
                param2 = null;
                _loc_8 = Vector3D(this._fighted).clone();
                _loc_8.w = 1;
            }
            if (!_loc_8)
            {
                _globlePos = _loc_9.world.position.clone();
                _loc_8 = _globlePos;
                _loc_8.w = _loc_10 ? (-1) : (1);
            }
            _loc_6[_loc_5] = _loc_8;
            return _loc_8;
        }// end function

        public function dispose(param1:Pivot3D) : void
        {
            this.unHangAll(param1);
            this._container = null;
            this._fighter = null;
            this._fighted = null;
            this._fightList = null;
            this.isOnPlaying = false;
            return;
        }// end function

        public function getObj3dByFlag(param1:String) : Pivot3D
        {
            var _loc_2:Pivot3D = null;
            if (param1 == ETempeItemName.FighterName)
            {
                _loc_2 = this._fighter;
            }
            else if (param1 == ETempeItemName.FightedName)
            {
                _loc_2 = this._fighted is Pivot3D ? (this._fighted) : (null);
            }
            else
            {
                _loc_2 = this._container.getObject3dByID(param1);
            }
            return _loc_2;
        }// end function

        public function getMeshByUid(param1:String) : Pivot3D
        {
            if (param1 == "parent0")
            {
                return this._fighter;
            }
            if (param1 == "parent2")
            {
                return this._fighted is Pivot3D ? (this._fighted) : (this._fightedObj);
            }
            else
            {
                if (param1 == "parent1")
                {
                    return this._weapon;
                }
                return null;
            }
        }// end function

        public function update() : void
        {
            return;
        }// end function

    }
}
